package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class FrameTestRun extends AppCompatActivity {
    private FrameView fView ;
    private AnimationDrawable run = null;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout fly = new FrameLayout(this);
        setContentView(fly);
        fView = new FrameView(this);
        fView.setBackgroundResource(R.drawable.loadingrun);
        fView.setVisibility(View.INVISIBLE);
        run = (AnimationDrawable) fView.getBackground();
        fView.setRun(run);
        fly.addView(fView);
        fly.setOnTouchListener(new View.OnTouchListener(){
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View view, MotionEvent event){
                //设置按下时才产生动画效果
                if (event.getAction() == MotionEvent.ACTION_DOWN){
                    run.stop();
                    float x = event.getX();
                    float y = event.getY();
                    fView.setLocation((int) y -40,(int) x-20);//View的显示位置
                    fView.setVisibility(View.VISIBLE);
                    run.start();//开启动画
                }
                return false;
            }
        });

    }


}